﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Dummy
{
    public class BattlefieldButtonInfo
    {
        public int i { get; private set; }
        public int j { get; private set; }
        public char? character { get; private set; }
        public bool isAdded { get; set; }

        private Button ui;
        private static BattlefieldButtonInfo[,] _field = new BattlefieldButtonInfo[5, 5];

        public BattlefieldButtonInfo(Grid _battlefieldUI, int _i, int _j, char? _character)
        {
            i = _i;
            j = _j;
            character = _character;

            ui = new Button();
            ui.FontSize = 35;
            ui.Background = null;
            ui.Click += ui_Click;
            _battlefieldUI.Children.Add(ui);
            Grid.SetColumn(ui, i);
            Grid.SetRow(ui, j);
        }

        private void ui_Click(object sender, RoutedEventArgs e)
        {
            if (BattlefieldGameInfo.IsCharAdded)
            {
                if (this.character.HasValue)
                {
                    BattlefieldGameInfo.AddCharacterToCurrentWord(this);
                    BattlefieldGameInfo.ShowSendButton();
                }
                else
                {
                    //error
                }
            }
            else if (this.character.HasValue)
            {
                //error
            }
            else if (!BattlefieldGameInfo.PickedCharacter.HasValue)
            {
                //error
            }
            else
            {
                this.SetCharacter(BattlefieldGameInfo.PickedCharacter);
                BattlefieldGameInfo.AddNewCharacterToBattlefield(this);
                BattlefieldGameInfo.ShowClearButton();
            }
        }

        public void SetCharacter(char? _character)
        {
            character = _character;
            ui.Content = _character;
        }

        public static void Init(Grid _battlefieldUI)
        {
            for (int i = 0; i < 5; i++)
                for (int j = 0; j < 5; j++)
                    _field[i, j] = new BattlefieldButtonInfo(_battlefieldUI, i, j, null);
        }

        public static void SetStartWord(string startWord)
        {
            for (int i = 0; i < 5; i++)
                _field[i, 2].SetCharacter(startWord[i]);
        }

        public static void DropIsAddedFlag()
        {
            for (int i = 0; i < 5; i++)
                for (int j = 0; j < 5; j++)
                    _field[i, j].isAdded = false;
        }

        public static void SwitchButtons(bool isEnabled)
        {
            for (int i = 0; i < 5; i++)
                for (int j = 0; j < 5; j++)
                    _field[i, j].ui.IsEnabled = isEnabled;
        }

        public static void AddCharacterToBattlefield(int moveI, int moveJ, char IJchar)
        {
            _field[moveI, moveJ].SetCharacter(IJchar);
        }
    }
}
